// Fill out your copyright notice in the Description page of Project Settings.

#include "SimpleProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Particles/ParticleSystem.h"
#include "Net/UnrealNetwork.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"


// Sets default values
ASimpleProjectile::ASimpleProjectile()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
	RootComponent = SphereComponent;
	SphereComponent->SetEnableGravity(false);
	SphereComponent->SetCollisionProfileName(TEXT("BlockAllDynamic"));
	SphereComponent->SetSphereRadius(20.f);

	ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ProjectileMeshComponent"));
	ProjectileMeshComponent->SetupAttachment(RootComponent);
	ProjectileMeshComponent->SetRelativeScale3D(FVector(2.f, 2.f, 2.f));
	ProjectileMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	ProjectileMeshComponent->SetEnableGravity(false);

	

	ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
	ProjectileMovementComponent->SetUpdatedComponent(SphereComponent);
	ProjectileMovementComponent->bRotationFollowsVelocity = true;
	ProjectileMovementComponent->InitialSpeed = 4000.f;
	ProjectileMovementComponent->MaxSpeed = 4000.f;
	ProjectileMovementComponent->bShouldBounce = false;
	ProjectileMovementComponent->ProjectileGravityScale = 0.f;

	
	SphereComponent->OnComponentHit.AddDynamic(this, &ASimpleProjectile::HandleOnComponentHit);
	
	bReplicates = true;

}

// Called when the game starts or when spawned
void ASimpleProjectile::BeginPlay()
{
	Super::BeginPlay();
	if (HasAuthority())
	{
		SetLifeSpan(5.f);
	}
}

void ASimpleProjectile::HandleOnComponentHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{

	if (this == OtherActor) return;
	
	SphereComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	if (GetNetMode() != NM_DedicatedServer && HitSound)
	{
		UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation());
	}
	if (!HasAuthority())
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionParticle, Hit.Location);
		ProjectileMeshComponent->SetVisibility(false);
	} else
	{ 
		//TArray<AActor*> IgnoreActors;
		//IgnoreActors.Add(this);
		//IgnoreActors.Add(GetOwner());
		//UGameplayStatics::ApplyRadialDamage(GetWorld(), ProjectileConfig.ExplosionDamage, Hit.Location, 10.f, ProjectileConfig.DamageType, IgnoreActors, this, GetInstigatorController());
		UGameplayStatics::ApplyDamage(OtherActor, ProjectileConfig.ExplosionDamage, GetInstigatorController(), this, ProjectileConfig.DamageType);
		
		ProjectileMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
		SetLifeSpan(1.f);
	}
}




// Called every frame
void ASimpleProjectile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ASimpleProjectile::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

}

